package com.example.mountainwalker.controller;

import com.example.mountainwalker.entity.UserInfo;
import com.example.mountainwalker.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mw")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    // 支持模糊查询和全部查询
    @GetMapping("/UserList")
    public List<UserInfo> getUserList(@RequestParam(required = false) String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return userService.getAllUsers();
        } else {
            return userService.getUserByKeyword(keyword);
        }
    }

    @GetMapping("/UserById")
    public UserInfo getUserById(Integer id) {
        return userService.getUserById(id);
    }

    @GetMapping("/deleteUserById")
    public int deleteUserById(Integer id) {
        return userService.deleteUserById(id);
    }

    @PostMapping("/addUser")
    public int addUser(@RequestBody UserInfo userInfo) {
        return userService.addUser(userInfo);
    }

    @PutMapping("/updateUser")
    public int updateUser(@RequestBody UserInfo userInfo) {
        return userService.updateUser(userInfo);
    }

}